package com.siren.tank.group;

import com.siren.tank.*;
import com.siren.tank.group.image.BadTank;

import java.awt.*;
import java.util.Map;

/**
 * @author : wangjichao
 * @description:
 * 坦克类：
 * 功能：
 *
 * @date : 2020/7/26 18:04
 */
public abstract class BaseTank extends GameObject {

    public static int WIDTH = ResourceMgr.tankL.getWidth();
    public static int HEIGHT = ResourceMgr.tankL.getHeight();

    private int x,y;
    private int oldX, oldY;
    private Dir dir;

    private Group group;

    private boolean live = true;

    /**
     * 根据按键控制tank的移动
     * @param keyEvents
     */
    public abstract void control(Map<Integer, Boolean> keyEvents);


    @Override
    public void paint(Graphics g) {
        boundCheck();
        draw(g);
    }

    /**
     * 子类实现tank的绘画逻辑
     * @param g
     */
    protected abstract void draw(Graphics g);

    public BaseTank(int x, int y, Dir dir) {
        this.x = x;
        this.y = y;
        this.dir = dir;
    }

    protected void boundCheck() {
        if (this.x < 0) {
            x = 0;
        }
        if (this.y < 30) {
            y = 30;
        }
        if (this.x > (TankFrame.WIDTH- BadTank.WIDTH)) {
            x = TankFrame.WIDTH- BadTank.WIDTH;
        }
        if (this.y > (TankFrame.HEIGHT - BadTank.HEIGHT)) {
            y = TankFrame.HEIGHT - BadTank.HEIGHT;
        }
    }


    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public boolean isLive() {
        return live;
    }

    public void setLive(boolean live) {
        this.live = live;
    }

    public int getOldX() {
        return oldX;
    }

    public void setOldX(int oldX) {
        this.oldX = oldX;
    }

    public int getOldY() {
        return oldY;
    }

    public void setOldY(int oldY) {
        this.oldY = oldY;
    }

    public void back() {
        this.x = this.oldX;
        this.y = this.oldY;
    }
}
